/**
 *
 * @author Richard Gathogo
 */

package org.prs.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.SchoolStage;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class SchoolStageDatabasePersistence {
    private Connection conn;
    private ResultSet result;
    public List<SchoolStage> getAllSchoolStages(){
        List<SchoolStage> schoolStages=new ArrayList<SchoolStage>();
        PreparedStatement selectAllSchoolStages=null;
        conn=MySQLDatabaseConnector.getConnection();
        try{
            selectAllSchoolStages=conn.prepareStatement("select * from schoolstage");
            result=selectAllSchoolStages.executeQuery();
            while(result.next()){
                schoolStages.add(getSchoolStage(result));
            }
        }catch(SQLException e){
            e.printStackTrace(System.out);
        }
        return  schoolStages;
    }
    public SchoolStage getSchoolStageById(int schoolStageId){

        SchoolStage schoolStage=new SchoolStage();
        PreparedStatement selectSchoolStageById=null;
        conn=MySQLDatabaseConnector.getConnection();
        try{
            selectSchoolStageById=conn.prepareStatement("select * from schoolstage where schoolstageid=?");
            selectSchoolStageById.setInt(1, schoolStageId);
            result=selectSchoolStageById.executeQuery();
            while(result.next()){
                schoolStage=getSchoolStage(result);
            
            }
        }catch(SQLException e){
            e.printStackTrace(System.out);
        }
        return  schoolStage;
    }
    public SchoolStage getSchoolStage(ResultSet result){

        SchoolStage schoolStage=new SchoolStage();
        try{
                schoolStage=new SchoolStage();
                schoolStage.setName(result.getString("name"));
                schoolStage.setSchoolStageId(result.getInt("schoolstageid"));
            
        }catch(SQLException e){
            e.printStackTrace(System.out);
        }
        return  schoolStage;
    }
}
